<?PHP
// Class to simplify Twitter API tasks
// Extends the EpiTwitter open source library
class SimpleTwitter extends EpiTwitter 
{
	
	public function __construct($consumerKey = null, $consumerSecret = null, $oauthToken = null, $oauthTokenSecret = null) {
		parent::__construct($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
		try {
			$creds = $this->get('/account/verify_credentials.json');
			$_SESSION['twitterName'] = $creds->screen_name;
			$_SESSION['twitterPic'] = $creds->profile_image_url;
		} catch(EpiTwitterException $e){} 
	}
	
	// Push user specified status onto Twitter, return true if no exceptions
	public function postStatus($status) {
		try{
			//$creds = $this->get('/account/verify_credentials.json');
			$resp = $this->post('/statuses/update.json', array('status' => $status));
			return $resp;
		}catch(EpiTwitterException $e){  
			return false;  
		}
	}
		
	
	// Return recent user tweets as an array of Tweet objects
	public function getRecentTweets() {
		// Verify user credentials and retrieve recent tweets
		try{
			//$creds = $this->get('/account/verify_credentials.json');
			$getTweets = $this->get('/statuses/user_timeline.json');
			$_SESSION['twitterLoggedIn'] = true;
		}catch(EpiTwitterException $e){  
			return false;  
		}
		$resp = $getTweets->response;
		
		$tweets = array();
		foreach($resp as $rawTweet) {
			$tweet = new Tweet($rawTweet['text'], $rawTweet['created_at']);
			$tweets[] = $tweet;
		}
		
		return $tweets;
	}
	
}

// Simple class to hold a tweet
class Tweet {
	protected $text;
	protected $postedOn;
	
	public function __construct($message = null, $createdOn = null) {
		$this->text = $message;
		$this->postedOn = $createdOn;
	}
	
	public function __get ($name) {
		if(property_exists($this, $name)) {
			return $this->$name;
		}
		return null;
	}
}

?>